package ngames.com.sushitv.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.temp.Adapter.MyBannerAdapter;
import ngames.com.sushitv.temp.Cache.BitmapCache;
import ngames.com.sushitv.temp.Class.Banner;
import ngames.com.sushitv.temp.Class.Featured;
import ngames.com.sushitv.temp.Interface.FeaturedInterface;
import ngames.com.sushitv.temp.Module.FeaturedFractory;
import ngames.com.sushitv.temp.Presenter.FeaturedPresenter;
import ngames.com.sushitv.temp.Thread.FeaturedBannerThread;
import ngames.com.sushitv.temp.Util.HttpUtil_featured;
import ngames.com.sushitv.temp.model.RankData;

/**
 * Created by edmond on 16-7-26.
 */
public class FeaturedFragment extends Fragment implements FeaturedInterface {

    private ImageView img1, img2, img3, img4, img5, img6;
    private TextView play1, play2, play3, play4, play5, play6;
    private TextView danmu1, danmu2, danmu3, danmu4, danmu5, danmu6;
    private TextView title1, title2, title3, title4, title5, title6;

    private ImageView aimg1, aimg2, aimg3, aimg4, aimg5, aimg6;
    private TextView aplay1, aplay2, aplay3, aplay4, aplay5, aplay6;
    private TextView adanmu1, adanmu2, adanmu3, adanmu4, adanmu5, adanmu6;
    private TextView atitle1, atitle2, atitle3, atitle4, atitle5, atitle6;

    private ImageView fimg1, fimg2, fimg3, fimg4, fimg5, fimg6;
    private TextView fplay1, fplay2, fplay3, fplay4, fplay5, fplay6;
    private TextView fdanmu1, fdanmu2, fdanmu3, fdanmu4, fdanmu5, fdanmu6;
    private TextView ftitle1, ftitle2, ftitle3, ftitle4, ftitle5, ftitle6;

    private static Handler mhandler;

    private View mView1, mView2,mView3;

    private View view;

    private ViewPager viewPager;
    private SwipeRefreshLayout swipeRefreshLayout;

    private List<ImageView> list;
    private LinearLayout linearLayout;

    private List<Banner> banners = new ArrayList<>();

    private MyBannerAdapter adapter;
    private BannerListener listener;

    private int pointIndex = 0;

    private FeaturedPresenter featuredPresenter;

    private AppApplication app = null;

    private FeaturedBannerThread thread;

    private Activity activity;

    private boolean isInit = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_featured,container,false);
        activity = getActivity();

        Log.d("FeaturedFragment","onCreate");

        Log.d("Fea","sss");

        FeaturedFractory featuredFractory = new FeaturedFractory();
        featuredPresenter = new FeaturedPresenter(featuredFractory,this, (AppApplication) activity.getApplication());

        app = (AppApplication) getActivity().getApplication();
        if(app.queue==null){
            app.queue = Volley.newRequestQueue(getActivity());
        }

        if(app.imageLoader==null){
            app.imageLoader = new ImageLoader(app.queue,new BitmapCache());
        }

        findViews();
        initData();
        initAction();

        mhandler = new featuredHandler();
        //initTop();
        /**
         * 下拉刷新的时候调用这一个东西就可以了
         * */
        HttpUtil_featured.featured_top(getContext());

        return view;
    }

    private void findViews(){
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        linearLayout = (LinearLayout) view.findViewById(R.id.points);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
    }

    private void initAction(){
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                featuredPresenter.refreshData();
            }
        });
    }

    private void initData(){
        featuredPresenter.initData();
    }

    private void setBanner(List<Banner> imageUrlsArray){
        if(isInit){
            viewPager.setOnPageChangeListener(null);
            linearLayout.removeAllViews();
            thread.setStop();
        }

        thread = new FeaturedBannerThread(activity,viewPager,5000);

        banners = imageUrlsArray;
        list = new ArrayList<>();
        View view;
        LinearLayout.LayoutParams params;

        Log.d("FeaturedFragment", String.valueOf(banners.get(0)+"fdsf"));

        for(int i=0;i<banners.size();i++){
            NetworkImageView imageView = new NetworkImageView(getActivity());
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            imageView.setDefaultImageResId(R.mipmap.ic_launcher);
            imageView.setErrorImageResId(R.mipmap.ic_launcher);
            Log.d("FeaturedFragment", String.valueOf(banners.size()));
            imageView.setImageUrl(banners.get(i).getImg_url(),app.imageLoader);
            list.add(imageView);

            view = new View(getActivity());
            params = new LinearLayout.LayoutParams(20,20);
            params.leftMargin = 10;
            view.setBackgroundResource(R.drawable.point_background);
            view.setLayoutParams(params);
            view.setEnabled(false);

            linearLayout.addView(view);
        }

        adapter = new MyBannerAdapter(list);
        viewPager.setAdapter(adapter);
        listener = new BannerListener();

        viewPager.setOnPageChangeListener(listener);
        int index = (Integer.MAX_VALUE/2) - (Integer.MAX_VALUE/2%list.size());
        viewPager.setCurrentItem(1000);
        Log.d("Featured", String.valueOf(pointIndex));
        thread.start();
        Log.d("pointIndex", String.valueOf(pointIndex));
        linearLayout.getChildAt(pointIndex).setEnabled(true);
    }

    @Override
    public void initedData() {
        isInit = true;
    }

    @Override
    public void initingData(List<Banner> bannersArray, List<Featured> featuredArray) {
        setBanner(bannersArray);
    }

    @Override
    public void refresinghData() {

    }

    @Override
    public void refreshedData(List<Banner> bannersArray, List<Featured> featuredArray) {
        setBanner(bannersArray);
        Log.d("FeaturedFragment","FreshEd");
        swipeRefreshLayout.setRefreshing(false);
    }

    class BannerListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

        @Override
        public void onPageSelected(int position) {
            int newPosition = position % banners.size();
            if(linearLayout.getChildAt(pointIndex)!=null){
                linearLayout.getChildAt(newPosition).setEnabled(true);
                linearLayout.getChildAt(pointIndex).setEnabled(false);
            }
            pointIndex = newPosition;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        Log.d("FeaturedFragment","onPause");
        thread.setStop();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    /************************************************************************************************ */
    public static void featured(int i) {
        Message msg;
        switch (i) {
            case 1://top
                msg = new Message();
                msg.what = 1;
                mhandler.sendMessage(msg);
                break;
            case 2://top
                msg = new Message();
                msg.what = 2;
                mhandler.sendMessage(msg);
                break;
            case 3://fanMade
                msg = new Message();
                msg.what = 3;
                mhandler.sendMessage(msg);
                break;
        }
    }

    /**
     * 初始化top的控件
     */
    private void initTop() {
        mView1 = view.findViewById(R.id.featuredFragment_top);
        play1 = (TextView) mView1.findViewById(R.id.text_6p_play_count1);
        play2 = (TextView) mView1.findViewById(R.id.text_6p_play_count2);
        play3 = (TextView) mView1.findViewById(R.id.text_6p_play_count3);
        play4 = (TextView) mView1.findViewById(R.id.text_6p_play_count4);
        play5 = (TextView) mView1.findViewById(R.id.text_6p_play_count5);
        play6 = (TextView) mView1.findViewById(R.id.text_6p_play_count6);

        danmu1 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count1);
        danmu2 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count2);
        danmu3 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count3);
        danmu4 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count4);
        danmu5 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count5);
        danmu6 = (TextView) mView1.findViewById(R.id.text_6p_danmu_count6);

        img1 = (ImageView) mView1.findViewById(R.id.relative_6p_img1);
        img2 = (ImageView) mView1.findViewById(R.id.relative_6p_img2);
        img3 = (ImageView) mView1.findViewById(R.id.relative_6p_img3);
        img4 = (ImageView) mView1.findViewById(R.id.relative_6p_img4);
        img5 = (ImageView) mView1.findViewById(R.id.relative_6p_img5);
        img6 = (ImageView) mView1.findViewById(R.id.relative_6p_img6);

        title1 = (TextView) mView1.findViewById(R.id.text_6p_title1);
        title2 = (TextView) mView1.findViewById(R.id.text_6p_title2);
        title3 = (TextView) mView1.findViewById(R.id.text_6p_title3);
        title4 = (TextView) mView1.findViewById(R.id.text_6p_title4);
        title5 = (TextView) mView1.findViewById(R.id.text_6p_title5);
        title6 = (TextView) mView1.findViewById(R.id.text_6p_title6);
        List<RankData> rankDataList = JSON.parseArray(AppApplication.getTempStr(), RankData.class);
        TextView[] plays = {play1, play2, play3, play4, play5, play6};
        TextView[] danmus = {danmu1, danmu2, danmu3, danmu4, danmu5, danmu6};
        ImageView[] images = {img1, img2, img3, img4, img5, img6};
        TextView[] titles = {title1, title2, title3, title4, title5, title6};
        for (int i = 0; i < 6; ++i) {
            RankData rank = rankDataList.get(i);
            plays[i].setText(rank.getPlay_count());
            danmus[i].setText(rank.getDanmu_count());
            titles[i].setText(rank.getTitle());
            HttpUtil_featured.setImage(images[i], rank.getCover(), getContext());
        }
        HttpUtil_featured.featured_anime(getContext());
    }

    /**
     * 初始化anime的控件
     */
    private void initAnime() {
        mView2 = view.findViewById(R.id.featuredFragment_anime);
        aplay1 = (TextView) mView2.findViewById(R.id.text_6p_play_count1);
        aplay2 = (TextView) mView2.findViewById(R.id.text_6p_play_count2);
        aplay3 = (TextView) mView2.findViewById(R.id.text_6p_play_count3);
        aplay4 = (TextView) mView2.findViewById(R.id.text_6p_play_count4);
        aplay5 = (TextView) mView2.findViewById(R.id.text_6p_play_count5);
        aplay6 = (TextView) mView2.findViewById(R.id.text_6p_play_count6);

        adanmu1 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count1);
        adanmu2 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count2);
        adanmu3 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count3);
        adanmu4 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count4);
        adanmu5 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count5);
        adanmu6 = (TextView) mView2.findViewById(R.id.text_6p_danmu_count6);

        aimg1 = (ImageView) mView2.findViewById(R.id.relative_6p_img1);
        aimg2 = (ImageView) mView2.findViewById(R.id.relative_6p_img2);
        aimg3 = (ImageView) mView2.findViewById(R.id.relative_6p_img3);
        aimg4 = (ImageView) mView2.findViewById(R.id.relative_6p_img4);
        aimg5 = (ImageView) mView2.findViewById(R.id.relative_6p_img5);
        aimg6 = (ImageView) mView2.findViewById(R.id.relative_6p_img6);

        atitle1 = (TextView) mView2.findViewById(R.id.text_6p_title1);
        atitle2 = (TextView) mView2.findViewById(R.id.text_6p_title2);
        atitle3 = (TextView) mView2.findViewById(R.id.text_6p_title3);
        atitle4 = (TextView) mView2.findViewById(R.id.text_6p_title4);
        atitle5 = (TextView) mView2.findViewById(R.id.text_6p_title5);
        atitle6 = (TextView) mView2.findViewById(R.id.text_6p_title6);
        List<RankData> rankDataList = JSON.parseArray(AppApplication.getTempStr(), RankData.class);
        TextView[] aplays = {aplay1, aplay2, aplay3, aplay4, aplay5, aplay6};
        TextView[] adanmus = {adanmu1, adanmu2, adanmu3, adanmu4, adanmu5, adanmu6};
        ImageView[] aimages = {aimg1, aimg2, aimg3, aimg4, aimg5, aimg6};
        TextView[] atitles = {atitle1, atitle2, atitle3, atitle4, atitle5, atitle6};
        for (int i = 0; i < 6; ++i) {
            RankData rank = rankDataList.get(i);
            aplays[i].setText(rank.getPlay_count());
            adanmus[i].setText(rank.getDanmu_count());
            atitles[i].setText(rank.getTitle());
            HttpUtil_featured.setImage(aimages[i], rank.getCover(), getContext());
        }
        HttpUtil_featured.featured_fanMade(getContext());
    }
    /**
     * 初始化fan made的控件
     */
    private void initFanMade() {
        mView3 = view.findViewById(R.id.featuredFragment_fanMade);
        fplay1 = (TextView) mView3.findViewById(R.id.text_6p_play_count1);
        fplay2 = (TextView) mView3.findViewById(R.id.text_6p_play_count2);
        fplay3 = (TextView) mView3.findViewById(R.id.text_6p_play_count3);
        fplay4 = (TextView) mView3.findViewById(R.id.text_6p_play_count4);
        fplay5 = (TextView) mView3.findViewById(R.id.text_6p_play_count5);
        fplay6 = (TextView) mView3.findViewById(R.id.text_6p_play_count6);

        fdanmu1 = (TextView) mView3.findViewById(R.id.text_6p_danmu_count1);
        fdanmu2 = (TextView) mView3.findViewById(R.id.text_6p_danmu_count2);
        fdanmu3 = (TextView) mView3.findViewById(R.id.text_6p_danmu_count3);
        fdanmu4 = (TextView) mView3.findViewById(R.id.text_6p_danmu_count4);
        fdanmu5 = (TextView) mView3.findViewById(R.id.text_6p_danmu_count5);
        fdanmu6 = (TextView) mView3.findViewById(R.id.text_6p_danmu_count6);

        fimg1 = (ImageView) mView3.findViewById(R.id.relative_6p_img1);
        fimg2 = (ImageView) mView3.findViewById(R.id.relative_6p_img2);
        fimg3 = (ImageView) mView3.findViewById(R.id.relative_6p_img3);
        fimg4 = (ImageView) mView3.findViewById(R.id.relative_6p_img4);
        fimg5 = (ImageView) mView3.findViewById(R.id.relative_6p_img5);
        fimg6 = (ImageView) mView3.findViewById(R.id.relative_6p_img6);

        ftitle1 = (TextView) mView3.findViewById(R.id.text_6p_title1);
        ftitle2 = (TextView) mView3.findViewById(R.id.text_6p_title2);
        ftitle3 = (TextView) mView3.findViewById(R.id.text_6p_title3);
        ftitle4 = (TextView) mView3.findViewById(R.id.text_6p_title4);
        ftitle5 = (TextView) mView3.findViewById(R.id.text_6p_title5);
        ftitle6 = (TextView) mView3.findViewById(R.id.text_6p_title6);
        List<RankData> rankDataList = JSON.parseArray(AppApplication.getTempStr(), RankData.class);
        TextView[] fplays = {fplay1, fplay2, fplay3, fplay4, fplay5, fplay6};
        TextView[] fdanmus = {fdanmu1, fdanmu2, fdanmu3, fdanmu4, fdanmu5, fdanmu6};
        ImageView[] fimages = {fimg1, fimg2, fimg3, fimg4, fimg5, fimg6};
        TextView[] ftitles = {ftitle1, ftitle2, ftitle3, ftitle4, ftitle5, ftitle6};
        for (int i = 0; i < 6; ++i) {
            RankData rank = rankDataList.get(i);
            fplays[i].setText(rank.getPlay_count());
            fdanmus[i].setText(rank.getDanmu_count());
            ftitles[i].setText(rank.getTitle());
            HttpUtil_featured.setImage(fimages[i], rank.getCover(), getContext());
        }
    }
    private class featuredHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1://top
                    initTop();
                    break;
                case 2://anime
                    initAnime();
                    break;
                case 3://fanMade
                    initFanMade();
                    break;
            }
        }
    }
}
